EmailAddresses are being saved as System.Object[]

Hi Guys,

I have written a powershell code and it requires some tweaking. All the values are being returned fine however emailaddresses is being coming as system.object[] how can i get the value to be outputted in csv format. I appreciate if someone can review and point out the error.

##################################################################################
# Powershell Script for Mailbox Data Collection 			                 
# by Navdeep www.Alivebits.com                                   
# Date 09 Jan 2015 version 1.0 Baseline                                                        
# Date 08 Feb 2015 version 1.1 Added Mailbox Statistics
# Date 29 June 2015 version 1.2 Updated Columns Database to ExchDB, WhenDataCol to 
# DataCollectionDate, mailbox size will return in MBs
##################################################################################

# Import Modules needed for Script

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

Set-AdServerSettings -viewEntireForest $true

$StartDate = get-date -format g

$sheetname= get-date -format "dd MMMM yyyy"
$sheetname.ToString()

<# 
Get-Mailbox -Resultsize unlimited | 
Select DisplayName, PrimarySMTPAddress, 
       @{Name=EmailAddresses;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq smtp} | 
       ForEach-Object {$_.SmtpAddress}}}, SamAccountName, ServerName, Database, Office, WhenMailboxCreated,@{Name="WhenDataCol"; Expression={$startDate}}, OrganizationalUnit |
       Export-CSV -Path C:\Users\exch2013\Documents\MailboxReport\MailboxReport_$sheetname.csv
#>

$DataPath = "C:\Users\PROFILE\Documents\MailboxReport\MailboxReport_$sheetname.csv"
$Results = @()
$MailboxUsers = get-mailbox -resultsize unlimited



foreach($user in $mailboxusers)
{
$UPN = $user.userprincipalname
$DN = $User.DisplayName
$PSMTP = $User.PrimarySMTPAddress
$EmailAddresses = $user.EmailAddresses | Where-Object {$_.PrefixString -ceq smtp} | ForEach-Object {$_.SmtpAddress}
$sAccName = $user.SamAccountName
$SN = $User.ServerName
$DB = $user.database
$OU = $user.OrganizationalUnit
$Office = $user.office
$MailboxCreated = $user.WhenCreated

if($user.ProhibitSendQuota -eq 'unlimited') 
    {
        $Quota=$user.ProhibitSendQuota
    }
else
    {
        $Quota=$user.ProhibitSendQuota.value.ToMB()
    }


$MbxStats = Get-MailboxStatistics $UPN

      $Properties = @{
						DisplayName = $DN
                        Office = $Office 			
                        OrganizationalUnit = $OU			
                        PrimarySMTPAddress = $PSMTP
						EmailAddresses = $EmailAddresses
						SamAccountName = $sAccName
						ServerName = $SN
						ExchDB = $DB
                        ProhibitSendQuota = $Quota
						TotalItemCount = $MbxStats.ItemCount
						TotalItemSize = $MbxStats.TotalItemSize.value.ToMB()
                        MailboxCreated = $MailboxCreated
                        LastLogonTime = $MbxStats.LastLogonTime						
                        DataCollectionDate = $startDate
						
                     }

$Results += New-Object psobject -Property $properties

}

$Results | Select-Object DisplayName,Office,OrganizationalUnit,PrimarySMTPAddress,EmailAddresses,SamAccountName,ServerName,ExchDB,ProhibitSendQuota,TotalItemSize,MailboxCreated,LastLogonTime,DataCollectionDate | Export-Csv -notypeinformation -Path $DataPath
July 1st, 2015 11:03am

I've seen this plenty of times myself and I always end up back at the same article by Boe Prox: http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/. You need to modify how you're setting your $EmailAddresses variable. I'd start by trying this:

$EmailAddresses = ($user.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp"} | ForEach-Object {$_.SmtpAddress}) -join ','
Edit: Fixed the smartquotes mentioned by Mike. Thanks, Mike!


Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 11:33am

Hi,

Give something like this a try:

$EmailAddresses = ($user.EmailAddresses | 
    Where-Object {$_.PrefixString -ceq 'smtp'} | 
        Select-Object -ExpandProperty SmtpAddress) -join ','
July 1st, 2015 11:37am

I've seen this plenty of times myself and I always end up back at the same article by Boe Prox: http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/. You need to modify how you're setting your $EmailAddresses variable. I'd start by trying this:

$EmailAddresses = ($user.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp"} | ForEach-Object {$_.SmtpAddress}) -join ','
Edit: Fixed the smartquotes mentioned by Mike. Thanks, Mike!


  • Proposed as answer by Mike Laughlin Wednesday, July 01, 2015 3:35 PM
  • Edited by tommymaynard Wednesday, July 01, 2015 4:45 PM
Free Windows Admin Tool Kit Click here and download it now
July 1st, 2015 3:31pm

I've seen this plenty of times myself and I always end up back at the same article by Boe Prox: http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/. You need to modify how you're setting your $EmailAddresses variable. I'd start by trying this:

$EmailAddresses = ($user.EmailAddresses | Where-Object {$_.PrefixString -ceq "smtp"} | ForEach-Object {$_.SmtpAddress}) -join ','
Edit: Fixed the smartquotes mentioned by Mike. Thanks, Mike!


  • Proposed as answer by Mike Laughlin Wednesday, July 01, 2015 3:35 PM
  • Edited by tommymaynard Wednesday, July 01, 2015 4:45 PM
July 1st, 2015 3:31pm

Hi Mike, Thanks for the solution. The changes you suggested worked, I have made a small modification, I have used -eq instead of -ceq as emailaddress would comes out to be blank for some users.

Regards,

Nav

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 2:09am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics